package org.example.事务消息.生产;

import lombok.SneakyThrows;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.TransactionMQProducer;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.common.message.Message;
import org.example.事务消息.监听.事务消息的监听;

/**
 * @Auther:Liu
 * @Testname:事务消息的生产
 * @Date:2025/8/23 9:39
 */
public class 事务消息的生产 {
    @SneakyThrows
    public static void main(String[] args) {
        // 创建事务类型的生产者
        TransactionMQProducer producer = new TransactionMQProducer("producer_group_c");
        // 设置NameServer的地址
        producer.setNamesrvAddr("192.168.10.88:9876");
        // 设置事务监听器
        producer.setTransactionListener(new 事务消息的监听());
        // 启动生产者
        producer.start();
        // 发送10条消息
        for (int i = 1; i < 11; i++) {
            String topic = "Topic_c";
            String tags = "transaction";
            String body = "Hello,Transaction!index:" + i;
            Message message = new Message(topic, tags, body.getBytes());
            message.setKeys("Num:" + i);
            producer.sendMessageInTransaction(message, null);
        }
    }
}
